// pages/user-register/index.js
const { $Message } = require('../../dist/base/index');
const handler = require("../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendtxt: "获取验证码",
    phone: null,
    phoneTest: false,
    verificationcode: null,
    verificationcodeTest: false,
    gettingCode: true,
    password: null,
    passwordTest: false,
    name:null,
    college:null,
    department:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  // phoneinput 获取电话号码
  phoneinput: function (e) {
    var test_phone = handler.regTest(e.detail.value, "请输入正确的电话号码格式", /^1\d{10}$/);
    test_phone && this.setData({ phone: e.detail.value });
  },
  // 发送验证码
  sendcode: function () {
    var that = this;
    if(this.data.phone!==null){
      handler.requestData("sendSMS", { phone: that.data.phone }, function (res) {
        if (res.success == true) {
          that.countDown()
          $Message({
            content: '验证码发送成功,有效期2小时',
            type: 'success'
          });
        } else {
          $Message({
            content: res.message,
            type: 'error'
          });
        }
      });
      console.log('触发发验证码')
      // wx.request({
      //   url: 'https://product.fishqc.com/Api/WeixinLogin/weixinminiMobileVerificationCode',
      //   data: {
      //     mobile: that.data.phone
      //   },
      //   method: 'POST',
      //   header: {
      //     'content-type': 'application/x-www-form-urlencoded' // 默认值
      //   },
      //   success: function (res) {
      //     console.log(res)
      //     if (res.data.code == 200) {
      //       that.countDown()
      //     } else {
      //       wx.showToast({
      //         title: '获取验证码失败',
      //         icon: "none"
      //       })
      //     }
      //   },
      //   fail: function () {
      //     wx.showToast({
      //       title: '获取验证码失败',
      //       icon: "none"
      //     })
      //   }
      // })
    }else{
      handler.regTest(this.data.phone, "请输入正确的电话号码格式", /^1\d{10}$/);
    }
  },

  //倒计时 倒数
  countDown: function () {
    var that = this;
    that.data.gettingCode = false;
    var seconds = 59;
    var countTimer = setInterval(function () {
      that.setData({
        sendtxt: seconds + "s"
      })
      seconds--;
      if (seconds <= 0) {
        countTimer = clearInterval(countTimer);
        that.setData({
          sendtxt: '获取验证码'
        })
        that.data.gettingCode = true;
      }
    }, 1000);
  },
  verificationinput: function (e) {
    this.setData({
      verificationcode: e.detail.value,
    });
    this.verificationTest(e.detail.value);
  },
  verificationTest: function (params) {
    if (params == null) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return;
    } else {
      this.setData({
        verificationcodeTest: true,
      });
    }
  },
  // passwordinput 获取密码
  passwordinput: function (e) {
    var test_password = handler.regTest(e.detail.value, "请输入密码", /^.{6,}$/);
    test_password && this.setData({ password: e.detail.value });
  },
  collageinput:function(e){
    var test_collage = handler.regTest(e.detail.value, "请输入学校");
    test_collage && this.setData({ collage: e.detail.value });
  },
  nameinput:function(e){
    var test_name = handler.regTest(e.detail.value, "请输入姓名");
    test_name && this.setData({ name: e.detail.value });
  },
  departmentinput:function(e){
    var test_department = handler.regTest(e.detail.value, "请输入部门");
    test_department && this.setData({ department: e.detail.value });
  },
  // getPhoneLogin 登录的电话号码
  getPhoneLogin: function () {
    const { sendtxt,
      phone,
      phoneTest,
      verificationcode,
      verificationcodeTest,
      password,
      passwordTest,
      name,
      collage,
      department } = this.data;
    // 验证信息
    
    var phone_test = handler.regTest(phone, "请输入电话号码", /^1\d{10}$/);
    var verificationcode_test = handler.regTest(verificationcode, "请输入验证码");
    var password_test = handler.regTest(password, "请输入密码", /^.{6,}$/)
    var name_test = handler.regTest(name, "请输入姓名")
    var college_test = handler.regTest(collage, "请输入学校")
    var department_test = handler.regTest(department, "请输入部门");
    var test_all = phone_test && verificationcode_test && password_test && name_test && college_test && department_test;
    var temp_user_type = wx.getStorageSync('user_type');
    var user_type = 0;
    if (temp_user_type == 'teacher'){
      user_type = 1
    }else{
      user_type = 0;
    }
  
    var submitData = {
      "phone": phone,
      "code": verificationcode,
      "password": password,
      "school": collage,
      "name" :name,
      "department": department,
      "user_type": user_type,
      "id": wx.getStorageSync('user_info')['id']
    };
    if (test_all) {
      var that = this;
      handler.requestData("userRegister", submitData, function (res) {
        if (res.success == true) {
           $Message({
            content: '注册成功',
            type: 'success'
          });
          handler.intoPageHandler("../login/index?type="+wx.getStorageSync('user_type'));
        } else {
          $Message({
            content: res.message,
            type: 'error'
          });
        }
      });
     
    } else {
      $Message({
        content: '注册失败',
        type: 'error'
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})